package com.youle.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youle.enums.ExceptionEnum;
import com.youle.exception.YLException;
import com.youle.item.pojo.Brand;
import com.youle.item.pojo.ItemCategory;
import com.youle.item.pojo.Spu;
import com.youle.item.vo.ItemVo;
import com.youle.mapper.BrandMapper;
import com.youle.mapper.ItemCategoryMapper;
import com.youle.mapper.SpuMapper;
import com.youle.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by
 *  item的service
 * @author touni
 * @date 2019/4/18
 * @time 14:58
 */
@Service
public class ItemSpuService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private ItemCategoryMapper itemCategoryMapper;


    /**
     * 注意SQL语句的问题
     * SELECT * FROM tb_spu WHERE saleable=0 AND (title LIKE '%手%' OR sub_title LIKE '%R15%')
     * SELECT * FROM tb_spu WHERE saleable=0 AND title LIKE '%手%' OR sub_title LIKE '%R15%'   由于OR的存在导致的
     *
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    public ResponseEntity<PageResult<ItemVo>> listItemSpu(String key, Boolean saleable, Integer page, Integer rows) {
        if (rows >0){ PageHelper.startPage(page,rows); }       //rows大于0的时候进行分页
        Spu spu = new Spu();
        if (null != saleable) { spu.setSaleable(saleable); }   //有值的时候才set
        List<Spu> spuList = null;
        if (StringUtils.isEmpty(key)){
             spuList = spuMapper.select(spu);
        }else {
            Example example = new Example(Spu.class);
            if (null == saleable) {
                example.createCriteria().andLike("title", "%" + key + "%")
                        .orLike("subTitle", "%" + key + "%");
                spuList = spuMapper.selectByExample(example);
            }else{ spuList = spuMapper.selectBykeyAndSaleable(saleable,"%"+key+"%","%"+key+"%"); }
        }
        if (CollectionUtils.isEmpty(spuList)){  throw new YLException(ExceptionEnum.NO_SPU_ITEM); }
        PageResult pageResult = new PageResult();
        //此处封装itemVo对象
        List<ItemVo> itemVoList = new ArrayList<>();
        for (Spu spua : spuList) {
            ItemVo itemVo = new ItemVo();
            itemVo.setId(spua.getId());
            itemVo.setTitle(spua.getTitle());
            Brand brand = new Brand();
            brand.setId(spua.getBrandId());
            brand = this.brandMapper.selectByPrimaryKey(brand);
            itemVo.setBname(brand.getName());
            Long cid1 = spua.getCid1();     //此处暂时只查询cid1 todo
            ItemCategory itemCategory = new ItemCategory();
            itemCategory.setId(cid1);
            itemCategory = itemCategoryMapper.selectByPrimaryKey(itemCategory);
            itemVo.setCname(itemCategory.getName());
            itemVoList.add(itemVo);
        }
        pageResult.setItems(itemVoList);
        if (rows > 0) {
            PageInfo pageInfo = new PageInfo(spuList);
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setTotalPage(pageInfo.getTotal() / rows);
        }else {
            pageResult.setTotal(Long.valueOf(spuList.size()));
            pageResult.setTotalPage(Long.valueOf(1));
        }
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(pageResult);
    }


    public ResponseEntity<Spu> queryDetailById(Long id) {
        Spu spu = new Spu();
        spu.setId(id);
        spu = spuMapper.selectByPrimaryKey(spu);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(spu);
    }






}
